﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Cariwisata.Data;

// The Item Detail Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234232

namespace Cariwisata
{
    /// <summary>
    /// A page that displays details for a single item within a group while allowing gestures to
    /// flip through other items belonging to the same group.
    /// </summary>
    public sealed partial class PanoramaPage : Cariwisata.Common.LayoutAwarePage
    {
        // Attribute
        double PosX;
        bool isTouch;
        Image image1, image2;

        public PanoramaPage()
        {
            this.InitializeComponent();
            PosX = 0;
            isTouch = false;
        }

        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="navigationParameter">The parameter value passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
        /// </param>
        /// <param name="pageState">A dictionary of state preserved by this page during an earlier
        /// session.  This will be null the first time a page is visited.</param>
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            // TODO: Create an appropriate data model for your problem domain to replace the sample data
            var group = SampleDataSource.GetGroup("Pariwisata");

            this.DefaultViewModel["Group"] = group;
            this.DefaultViewModel["Items"] = group.Items;

            if (navigationParameter != null)
            {
                this.flipView.SelectedItem = SampleDataSource.GetItem((string)navigationParameter);
            }
        }

        /// <summary>
        /// Preserves state associated with this page in case the application is suspended or the
        /// page is discarded from the navigation cache.  Values must conform to the serialization
        /// requirements of <see cref="SuspensionManager.SessionState"/>.
        /// </summary>
        /// <param name="pageState">An empty dictionary to be populated with serializable state.</param>
        protected override void SaveState(Dictionary<String, Object> pageState)
        {   
            var selectedItem = this.flipView.SelectedItem;
            // TODO: Derive a serializable navigation parameter and assign it to pageState["SelectedItem"]
        }

        private void ImageGrid_PointerMoved(object sender, PointerRoutedEventArgs e)
        {
            double tempX = e.GetCurrentPoint((Grid)sender).Position.X - PosX;
            if (tempX != 0 && isTouch)
            {
                int width = 0;
                if (((SampleDataItem)flipView.SelectedItem).Nama.Equals("Prambanan"))
                { 
                    width = 2300 ;
                }
                else if (((SampleDataItem)flipView.SelectedItem).Nama.Equals("Pulau Komodo"))
                {
                    width = 2000;
                }
                else
                {
                    width = 1500;
                }
                int leftBorder = -(width >> 1);
                int rightBorder = width >> 1;

                image1.Margin = new Thickness(image1.Margin.Left + tempX, 0, 0, 0);
                if (image1.Margin.Left < 0)
                {
                    image2.Margin = new Thickness(image1.Margin.Left + width, 0, 0, 0);
                }
                else if (image1.Margin.Left > 0)
                {
                    image2.Margin = new Thickness(image1.Margin.Left - width, 0, 0, 0);
                }

                if (image1.Margin.Left < leftBorder || image1.Margin.Left > rightBorder)
                {
                    Image temp = image2;
                    image2 = image1;
                    image1 = temp;
                }

                PosX = e.GetCurrentPoint((Grid)sender).Position.X;
            }
            
        }

        private void ImageGrid_PointerPressed(object sender, PointerRoutedEventArgs e)
        {
            isTouch = true;
            PosX = e.GetCurrentPoint((Grid)sender).Position.X;
            Grid imageGrid = (Grid)sender;
            image1 = ((Image)imageGrid.Children[0]);
            image2 = ((Image)imageGrid.Children[1]);
            if (((SampleDataItem)flipView.SelectedItem).Nama.Equals("Prambanan"))
            {
                imageGrid.Width = 4600;
            }
            else if (((SampleDataItem)flipView.SelectedItem).Nama.Equals("Pulau Komodo"))
            {
                imageGrid.Width = 4000;
            }
            else
            {
                imageGrid.Width = 3000;
            }
        }

        private void ImageGrid_PointerReleased(object sender, PointerRoutedEventArgs e)
        {
            isTouch = false;
        }

    }
}
